﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices.Automation;

namespace ComToolkit.IO
{
    //http://msdn.microsoft.com/en-us/library/7kby5ae3%28VS.85%29.aspx
    //http://www.w3schools.com/asp/asp_ref_folder.asp
    public static class FileSystemObject
    {
        private static dynamic fileSystemObject;

        static FileSystemObject()
        {
            switch (System.Environment.OSVersion.Platform)
            {
                case PlatformID.Win32NT:
                case PlatformID.Win32Windows:
                    break;
                default:
                    {
                        throw new NotImplementedException("ComToolkit cannot run on platform: " + System.Environment.OSVersion.Platform);
                    }
            }
            if (AutomationFactory.IsAvailable)
            {
                try
                {
                    fileSystemObject = AutomationFactory.GetObject("Scripting.FileSystemObject");
                }
                catch
                {
                    try
                    {
                        fileSystemObject = AutomationFactory.CreateObject("Scripting.FileSystemObject");
                    }
                    catch
                    {
                    }
                }
            }
            else
            {
                fileSystemObject = null;
            }
        }

        public static dynamic Get()
        {
            return fileSystemObject;
        }

        public static void ForEach<T>(this IEnumerable<T> items, Action<T> action)
        {
            foreach (T item in items)
            {
                action(item);
            }
        }
    }
}
